# Top level -*- makefile -*- fragment for GCC-LLVMIR
#
# Copyright (C) 2011,2012 Matthew Wahab <mwb.cde@gmail.com>
#
# This file is part of GCC-LLVMIR.
#
# GCC-LLVMIR is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GCC-LLVMIR is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GCC-LLVMIR; see the file COPYING3.  If not see
# <http://www.gnu.org/licenses/>.

##
# Variables

# LLVMIR_USE_CLANG: Define to include Clang support
LLVMIR_USE_CLANG:=yes

# The name of the LLVMIR compiler.
LLVMIR_EXE = llvmir1$(exeext)

# The LLVMIR-specific object files included in $(LLVMIR_EXE).
LLVMIR_OBJS = llvmir/llvmir.o \
	llvmir/asmparser.o llvmir/external.o \
	llvmir/convert.o llvmir/convert_functions.o \
	llvmir/convert_instrs.o llvmir/convert_types.o \
	llvmir/llvmir-lang.o

# Object files required by the GCC frontend interface.
LLVMIR_REQ_OBJS = attribs.o diagnostic.o 

# Files the build depends on:
LLVMIR_BUILD_FILES=llvmir/Make-lang.in

# Files required for the GCC frontend interface.
LLVMIR_REQ_FILES = $(CONFIG_H) $(SYSTEM_H) opts.h coretypes.h \
		langhooks.h $(LANGHOOKS_DEF_H) \
		debug.h errors.h $(GGC_H) $(TARGET_H) \
		$(DIAGNOSTIC_H) $(TOPLEV_H) $(TIMEVAR_H) $(TREE_H)

# LLVMIR headers
LLVMIR_HEADERS=llvmir/llvmir.h llvmir/llvmir-system.h llvmir/llvmir-c.h \
	llvmir/repr.h llvmir/parser.h \
	llvmir/asmparser.h 

# Directory include flags (-I)
LLVMIR_INCLUDES=-I $(srcdir)/llvmir

# llvm-config executable. 
LLVMCONFIG?=llvm-config

# The Clang libraries.
CLANG_MODULES:=\
	FrontendTool Frontend Driver \
	Serialization CodeGen Parse Sema \
	StaticAnalyzerFrontend StaticAnalyzerCheckers StaticAnalyzerCore \
	Analysis Index Rewrite ARCMigrate \
	AST Lex Basic

ifdef LLVMIR_USE_CLANG
CLANG_LIBS:=$(addprefix -lclang,$(CLANG_MODULES))
LLVM_MODULES+=
endif

# The LLVM headers and libraries.
LLVMIR_LIBDIR=$(shell $(LLVMCONFIG) --libdir)
LLVMIR_CXXFLAGS:=$(shell $(LLVMCONFIG) --cxxflags) 
LLVMIR_LDFLAGS:=$(shell $(LLVMCONFIG) --ldflags)

LLVM_MODULES:=core support bitreader asmparser asmprinter

ifeq ("$(strip $(LLVMIR_USE_CLANG))","yes")
LLVM_MODULES+= \
	ipo \
	analysis \
	linker \
	codegen \
	transformutils \
	instrumentation \
	bitwriter 

LLVMIR_CXXFLAGS+=-DLLVMIR_USE_CLANG=1
endif

LLVMIR_LIBS:=$(CLANG_LIBS) \
	$(shell $(LLVMCONFIG) --libs $(LLVM_MODULES))

#$(info LLVMCONFIG=$(LLVMCONFIG))
#$(info LLVMIR_CXXFLAGS=$(LLVMIR_CXXFLAGS))
#$(info LLVMIR_LDFLAGS=$(LLVMIR_LDFLAGS))
#$(info LLVMIR_LIBS=$(LLVMIR_LIBS))

# Rules

# These hooks are used by the main GCC Makefile.  Consult that
# Makefile for documentation.
llvmir.all.cross: $(LLVMIR_EXE)
llvmir.start.encap: $(LLVMIR_EXE)
llvmir.rest.encap:
llvmir.tags:
llvmir.install-common:
llvmir.install-man:
llvmir.install-info:
llvmir.dvi:
llvmir.pdf:
llvmir.install-pdf:
llvmir.html:
llvmir.install-html:
llvmir.uninstall:
llvmir.info:
llvmir.man:
llvmir.srcextra:
llvmir.srcman:
llvmir.srcinfo:
llvmir.install-plugin:

llvmir.mostlyclean:
	rm -f $(LLVMIR_OBJS) $(LLVMIR_EXE)

llvmir.clean:
llvmir.distclean:
llvmir.maintainer-clean:
llvmir.stage1:
llvmir.stage2:
llvmir.stage3:
llvmir.stage4:
llvmir.stageprofile:
llvmir.stagefeedback:

# LLVMIR rules.

# Use strict warnings for this front end.
llvmir-warn = $(STRICT_WARN)

#.PHONEY: $(LLVMIR_EXE)
#$(LLVMIR_EXE):

$(LLVMIR_EXE): $(LLVMIR_OBJS) $(LLVMIR_REQ_OBJS) $(BACKEND) $(LIBDEPS)
	$(CXX) \
	$(ALL_LINKERFLAGS) $(LDFLAGS) $(LLVMIR_LDFLAGS) \
	-o $@ \
	$(LLVMIR_REQ_OBJS) $(LLVMIR_OBJS) \
	$(BACKEND) $(BACKENDLIBS)  \
	$(EXTRA_GCC_LIBS) $(LIBS) \
	$(LLVMIR_LIBS) 

# Use the C++ compiler for .cc files
llvmir/%.o: llvmir/%.cc
	$(CXX) -c $(LLVMIR_INCLUDES) $(ALL_CPPFLAGS) \
		$(LLVMIR_CXXFLAGS) $(ALL_CXXFLAGS) \
		$< $(OUTPUT_OPTION)

# Dependencies for C files
llvmir/llvmir-lang.o: llvmir/llvmir-lang.c $(LLVMIR_REQ_FILES) \
	gtype-llvmir.h gt-llvmir-llvmir-lang.h $(LLVMIR_BUILD_FILES)

# Dependencies for C++ files
llvmir/llvmir.o: llvmir/llvmir.cc $(LLVMIR_REQ_FILES) \
	$(LLVMIR_HEADERS) $(LLVMIR_BUILD_FILES)

llvmir/asmparser.o: llvmir/asmparser.cc $(LLVMIR_REQ_FILES) \
	$(LLVMIR_HEADERS) $(LLVMIR_BUILD_FILES)

llvmir/external.o: llvmir/external.cc $(LLVMIR_REQ_FILES) \
	$(LLVMIR_HEADERS) $(LLVMIR_BUILD_FILES)

llvmir/convert.o: llvmir/convert.cc $(LLVMIR_REQ_FILES) \
	$(LLVMIR_HEADERS) $(LLVMIR_BUILD_FILES)

llvmir/convert_functions.o: llvmir/convert_functions.cc $(LLVMIR_REQ_FILES) \
	$(LLVMIR_HEADERS) $(LLVMIR_BUILD_FILES)

llvmir/convert_types.o: llvmir/convert_types.cc $(LLVMIR_REQ_FILES) \
	$(LLVMIR_HEADERS) $(LLVMIR_BUILD_FILES)

llvmir/convert_instrs.o: llvmir/convert_instrs.cc $(LLVMIR_REQ_FILES) \
	$(LLVMIR_HEADERS) $(LLVMIR_BUILD_FILES)
